package com.wanjia.seal.manager.controller;

import com.alibaba.fastjson.JSONObject;
import com.wanjia.seal.manager.annotation.PassToken;
import com.wanjia.seal.manager.controller.ex.DataNotFoundException;
import com.wanjia.seal.manager.controller.ex.yanzhenException;
import com.wanjia.seal.manager.controller.ex.yanzhennotfoundException;
import com.wanjia.seal.manager.entity.BankInfoTB;
import com.wanjia.seal.manager.entity.OperUser;
import com.wanjia.seal.manager.entity.OperUserVo;
import com.wanjia.seal.manager.entity.OperatorUser;
import com.wanjia.seal.manager.mapper.BankInfoTBMapper;
import com.wanjia.seal.manager.mapper.OperatorUserMapper;
import com.wanjia.seal.manager.service.Impl.TokenServiceA;
import com.wanjia.seal.manager.service.OperatorUserService;
import com.wanjia.seal.manager.service.TokenService;
import com.wanjia.seal.manager.utils.JsonResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;



@Controller
@Api(tags = "额外登陆")
@Slf4j
public class OperUserController extends BaseController{

    @Value("${OTHER_URL}")
    private String URL;

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    OperatorUserService operatorUserService;
    @Autowired
    OperatorUserMapper operatorUserMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private BankInfoTBMapper bankInfoTBMapper;


    @ResponseBody
    @PostMapping("/v0/authverify")
    public JsonResult<Object> testPost(@RequestBody OperUser operUser){
        String userid = operUser.getUserid();
        String branchid = operUser.getBranchid();
        String authcode = operUser.getAuthcode();
        log.info("userid是：{}",userid);
        log.info("branchid是：{}",branchid);
        log.info("authcode是：{}",authcode);
        log.info("版本是7");
        OperUserVo operUserVo= new OperUserVo();
        operUserVo.setOper_no(userid);
        operUserVo.setInst_no(branchid);
        operUserVo.setAuth_code(authcode);
        if (URL.isEmpty()){
            //BankInfoTB bankInfoTB2 = bankInfoTBMapper.selectByBankIDs(branchid);
            OperatorUser data = operatorUserMapper.selectById("9999");
            String token = tokenService.getToken(data);
            BankInfoTB bankInfoTB = bankInfoTBMapper.selectById(branchid);
            log.info("operatorUser是：{}",data);
            log.info("token是：{}",token);
            log.info("bankInfoTB是：{}",bankInfoTB);
            JSONObject BB =new JSONObject();
            BB.put("token",token);
            BB.put("Bank",bankInfoTB);
            return new JsonResult<>(OK,BB);
        }else{
            log.info("开启远程连接，如果成功会显示远程连接结束");
            String user = restTemplate.postForObject(URL, operUserVo, String.class);
            log.info("远程连接结束");
            JSONObject jsonObject = JSONObject.parseObject(user);
            String status = JSONObject.parseObject(jsonObject.getString("data")).getString("status");
            log.info("status是:{}",status);
            log.debug("报文：{}",user);
            System.out.println(status);
            if (!"0".equals(status)){
                throw new yanzhenException("验证不通过");
            }
            OperatorUser data = operatorUserMapper.selectById("9999");
            String token = tokenService.getToken(data);
            BankInfoTB bankInfoTB = bankInfoTBMapper.selectById(branchid);
            log.info("operatorUser是：{}",data);
            log.info("token是：{}",token);
            log.info("bankInfoTB是：{}",bankInfoTB);
            JSONObject BB =new JSONObject();
            BB.put("token",token);
            BB.put("Bank",bankInfoTB);
            return new JsonResult<>(OK,BB);
        }
    }
}
